package com.rrd.itext.xmlHandler;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import com.rrd.itext.utils.GlobalNames;
import com.rrd.itext.xmlBean.dataBean.DataXmlBaseBean;
import com.rrd.itext.xmlBean.fontBean.XmlFonts;
import com.rrd.itext.xmlBean.layoutBean.XmlBaseBean;
import com.rrd.itext.xmlBean.layoutBean.XmlHtmlBlock;
import com.rrd.itext.xmlBean.layoutBean.XmlPage;
import com.rrd.itext.xmlBean.layoutBean.XmlSection;
import com.rrd.itext.xmlBean.layoutBean.XmlZone;
import com.rrd.itext.xmlBean.wrapper.DataXStreamWrapper;
import com.rrd.itext.xmlBean.wrapper.FontXStreamWrapper;
import com.rrd.itext.xmlBean.wrapper.LayoutXStreamWrapper;

public class PDFLayoutHandler extends XmlHandler{
	private String resPath;
	private String dataXML;
	
	public PDFLayoutHandler(String xmlPath, String dataXML){
		this.resPath = xmlPath;
		this.dataXML = dataXML;
	}
	
	
	public void handleMessage(XmlHanlderCallBack handler, java.io.OutputStream output) throws Exception {
        File layoutFile = new File(this.resPath + GlobalNames.LAYOUT_FILE_NAME);
        File fontFile = new File(this.resPath + GlobalNames.FONT_FILE_NAME);
        File dataFile = new File(this.resPath + GlobalNames.DATA_FILE_NAME);
		try {
		    BufferedReader layoutBR = new BufferedReader(new InputStreamReader(new FileInputStream(layoutFile), "UTF-8"));

            LayoutXStreamWrapper layoutWwrapper = new LayoutXStreamWrapper();
            XmlBaseBean xLayout = (XmlBaseBean) layoutWwrapper.initXStream().fromXML(layoutBR);
            
            BufferedReader dataBR;
            if(this.dataXML != null){
                dataBR = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(dataXML.getBytes())));
            }else{
                dataBR = new BufferedReader(new InputStreamReader(new FileInputStream(dataFile), "UTF-8"));
            }
            DataXStreamWrapper dataWrapper = new DataXStreamWrapper();
            DataXmlBaseBean xData = (DataXmlBaseBean) dataWrapper.initXStream().fromXML(dataBR);
            
            BufferedReader fontBR = new BufferedReader(new InputStreamReader(new FileInputStream(fontFile), "UTF-8"));
            FontXStreamWrapper xFont = new FontXStreamWrapper();
            XmlFonts xFonts = (XmlFonts)xFont.initXStream().fromXML(fontBR);
            
            handler.processLayout(xLayout);
            handler.processData(xData);
            handler.processFonts(xFonts);
            handler.callBack(output);
		}catch(Exception e){
		    e.printStackTrace();
		    throw e;
		}
	}


    public String getDataXML() {
        return dataXML;
    }


    public void setDataXML(String dataXML) {
        this.dataXML = dataXML;
    }
}
